package chenjiuxu.seniorui.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

/**
 * Created by 15705 on 2018/1/15.
 * 截取进度条
 */
public class DoubleProgressBar extends View {


    private final Paint paint;
    private float right;//
    private float left;
    private int width = -1;
    private int height = -1;

    private int margin = 15;

    private int selectColor = Color.RED;
    private int noSelectColor = Color.BLACK;
    private int dotColor = Color.BLUE;

    private int strokeWidth = 20;//中线宽度
    private int dotWidth = 20;//点的宽度
    private boolean isLeft;
    private int max;//最大进度
    private int maxSize;//两点之间最大距离
    private OnDoubleProgressListener listener;//监听器

    public DoubleProgressBar(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        paint = new Paint();
        paint.setAntiAlias(true);
    }

    /**
     * 设置追到进度
     */
    public void setMax(int max) {
        this.max = max;
    }

    /**
     * 设置最大间距
     */
    public void setMaxSize(int maxSize) {
        this.maxSize = maxSize;
    }


    @Override
    protected void onDraw(Canvas canvas) {
        if (width == -1) {
            width = getWidth();
            height = getHeight();
            left = 200;
            right = 200;
        }
        int h = height / 2;
//画线
        paint.setStrokeWidth(strokeWidth);
        paint.setStrokeCap(Paint.Cap.ROUND);
        paint.setColor(noSelectColor);
        canvas.drawLine(margin, h, left, h, paint);//左线
        canvas.drawLine(right, h, width - margin, h, paint);//右线

        paint.setStrokeCap(Paint.Cap.BUTT);
        paint.setColor(selectColor);
        canvas.drawLine(left, h, right, h, paint);//选择线


//画点
        paint.setStrokeCap(Paint.Cap.ROUND);
        paint.setColor(dotColor);
        paint.setStrokeWidth(dotWidth);

        int sw = strokeWidth / 2;
        int y1 = h + sw;
        int y2 = h - sw;
        int dw = dotWidth / 2;
        canvas.drawLine(left - dw, y1, left - dw, y2, paint);//左点
        canvas.drawLine(right + dw, y1, right + dw, y2, paint);//右点
    }


    @Override
    public boolean onTouchEvent(MotionEvent event) {
        float x;
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                x = event.getX();
                isLeftAnRight(x);
                if (isLeft)
                    left = x;
                else
                    right = x;
                dispose();
                invalidate();
                setListener();
                break;
            case MotionEvent.ACTION_MOVE:
                x = event.getX();
                if (isLeft)
                    left = x;
                else
                    right = x;
                dispose();
                invalidate();
                setListener();
                break;
            case MotionEvent.ACTION_UP:
                x = event.getX();
                if (isLeft)
                    left = x;
                else
                    right = x;
                dispose();
                invalidate();
                setEndListener();
                break;
        }
        return true;
    }

    /**
     * 调用监听
     */
    private void setListener() {
        int l = calculationSchedule(left);
        int r = calculationSchedule(right);
        listener.onDoubleProgressListener(l, r, r - l);
    }

    /**
     * 结束监听
     */
    private void setEndListener() {
        int l = calculationSchedule(left);
        int r = calculationSchedule(right);
        listener.onEndDoubleProgressListener(l, r, r - l);
    }


    /**
     * 计算进度
     */
    private int calculationSchedule(float progress) {
        //   progress -= margin;
        float w = width - margin * 2;
        float p = progress / w;
        return (int) (max * p);
    }


    //左右点的约束
    private void dispose() {
        float ms = ((1f * maxSize / max) * (width - margin * 2));
        if (isLeft) {
            if (margin > left) {
                left = margin;
            } else if (width - margin < left) {
                left = width - margin;
            }
            float rl = right - left;
            if (rl < 0) right = left;
            if (rl > ms) {
                right = (int) (left + ms);
            }
        } else {
            if (margin > right) {
                right = margin;
            } else if (width - margin < right) {
                right = width - margin;
            }
            float rl = right - left;
            if (rl < 0) left = right;
            if (rl > ms) {
                left = (int) (right - ms);
            }
        }
    }


    //判断是左右
    private void isLeftAnRight(float x) {
        float centre = (right - left) / 2 + left;
        if (centre >= x) {
            isLeft = true;
        } else {
            isLeft = false;
        }
    }

    /**
     * 设置进度监听
     */
    public void setOnDoubleProgressListener(OnDoubleProgressListener listener) {
        this.listener = listener;
    }

    /**
     * 监听
     */
    public interface OnDoubleProgressListener {
        void onDoubleProgressListener(int left, int right, int size);

        void onEndDoubleProgressListener(int left, int right, int size);
    }
}
